import { createApp } from 'vue';
import App from './App.vue';
import Antd from 'ant-design-vue';
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
import router from './routes/index';
import store from './store/index';
import 'ant-design-vue/dist/antd.css';
import XiguaComponents from 'xigua-componentss';
import 'xigua-componentss/dist/bundle.css';
import 'cropperjs/dist/cropper.css';
import { RespData } from './store/respTypes';
export type ICustomAxiosConfig = AxiosRequestConfig & {
    opName?: string;
};
const app = createApp(App);
const baseBackendURL = 'http://localhost:3000'
export const baseH5URL = 'http://182.92.168.192:8082'
axios.defaults.baseURL = `${baseBackendURL}/api/`
axios.interceptors.request.use(config => {
  const newConfig = config as ICustomAxiosConfig
  store.commit('setError', { status: false, message: '' })
  store.commit('startLoading', { opName: newConfig.opName })
  return config
})
axios.interceptors.response.use((resp: AxiosResponse<RespData<unknown>>) => {
  const { config, data } = resp
  const newConfig = config as ICustomAxiosConfig
  store.commit('finishLoading', { opName: newConfig.opName })
  const { errno, message } = data
  if (errno !== 0) {
    store.commit('setError', { status: true, message })
    return Promise.reject(data)
  }
  return resp
}, (e: AxiosError) => {
  const newConfig = e.config as ICustomAxiosConfig
  store.commit('setError', { status: true, message: '服务器错误' })
  store.commit('finishLoading', { opName: newConfig.opName })
  return Promise.reject(e)
})
app.use(Antd).use(router).use(store).use(XiguaComponents);
app.mount('#app');
